package com.example.lenjor.sprite;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.example.lenjor.sprite.adapter.SubViewAdapter;
import com.example.lenjor.sprite.util.AnimUtil;
import com.example.lenjor.sprite.view.BeiBaoView;
import com.example.lenjor.sprite.view.ShopView;
import com.example.lenjor.sprite.view.ShouYeView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity{

    private RelativeLayout rl_root;
    private Button btn_message;
    private FrameLayout fl_fold_btn;
    private FrameLayout fl_expand_btn;
    //伸缩框
    private FrameLayout fl_beibao;
    private FrameLayout fl_rongyu;
    private FrameLayout fl_liuyan;
    private FrameLayout fl_renwu;
    private FrameLayout fl_chuzhan;
    private FrameLayout fl_hecheng;
    //底部框
    private Button btn_home;
    private Button btn_level;
    private View btn_raid;
    private Button btn_shop;
    private Button btn_friends;
    private Button btn_setting;
    private LinearLayout ll_sub_btns;
    private ProgressBar pb_timer_l;

    private ViewPager vp_content;
    private List<View> subViewList;
    private ShouYeView shouYeView;
    private ShopView shopView;
    private BeiBaoView beiBaoView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        updateTimer();
        initSubViewList();
    }
    private void initSubViewList() {
        subViewList = new ArrayList<View>();
        shouYeView = new ShouYeView(this);
        shopView = new ShopView(this);

        subViewList.add(shouYeView.getView());
        subViewList.add(shopView.getView());
        SubViewAdapter adapter = new SubViewAdapter(getApplicationContext(), subViewList);
        vp_content.setAdapter(adapter);

    }

    private void initUI() {
        rl_root = (RelativeLayout) findViewById(R.id.rl_root);

        fl_fold_btn = (FrameLayout) findViewById(R.id.fl_fold_btn);
        String[] proNames = new String[]{AnimUtil.PROPERTY_SCALEX, AnimUtil.PROPERTY_SCALEY};
        AnimUtil.addOnTouchListener(fl_fold_btn, proNames,
                new float[]{1f, 0.75f}, new float[]{0.75f, 1f}, animListener);
        fl_expand_btn = (FrameLayout) findViewById(R.id.fl_expand_btn);

        btn_message = (Button) findViewById(R.id.btn_message);
        AnimUtil.addOnTouchListener(btn_message, 0f, 0f, null);

        fl_beibao = (FrameLayout) findViewById(R.id.fl_beibao);
        AnimUtil.addOnTouchListener(fl_beibao, 0.5f, 1f, animListener);
        beiBaoView = new BeiBaoView(this);

        fl_rongyu = (FrameLayout) findViewById(R.id.fl_rongyu);
        AnimUtil.addOnTouchListener(fl_rongyu, 0.5f, 1f, null);

        fl_liuyan = (FrameLayout) findViewById(R.id.liuyan);
        AnimUtil.addOnTouchListener(fl_liuyan, 0.5f, 1f, null);

        fl_renwu = (FrameLayout) findViewById(R.id.renwu);
        AnimUtil.addOnTouchListener(fl_renwu, 0.5f, 1f, null);

        fl_chuzhan = (FrameLayout) findViewById(R.id.chuzhan);
        AnimUtil.addOnTouchListener(fl_chuzhan, 0.5f, 1f, null);

        fl_hecheng = (FrameLayout) findViewById(R.id.hecheng);
        AnimUtil.addOnTouchListener(fl_hecheng, 0.5f, 1f, null);

        btn_home = (Button) findViewById(R.id.btn_home);
        AnimUtil.addOnTouchListener(btn_home, 0.5f, 1f, animListener);

        btn_level = (Button) findViewById(R.id.btn_level);
        AnimUtil.addOnTouchListener(btn_level, 0.5f, 1f, null);

        btn_raid = findViewById(R.id.btn_raid);
        AnimUtil.addOnTouchListener(btn_raid, 0.5f, 1f, null);

        btn_shop = (Button) findViewById(R.id.btn_shop);
        AnimUtil.addOnTouchListener(btn_shop, 0.5f, 1f, animListener);

        btn_friends = (Button) findViewById(R.id.btn_friends);
        AnimUtil.addOnTouchListener(btn_friends, 0.5f, 1f, animListener);

        btn_setting = (Button) findViewById(R.id.btn_setting);
        AnimUtil.addOnTouchListener(btn_setting, 0.5f, 1f, animListener);

        ll_sub_btns = (LinearLayout) findViewById(R.id.ll_sub_btns);

        pb_timer_l = (ProgressBar) findViewById(R.id.pb_timer_l);

        vp_content = (ViewPager) findViewById(R.id.vp);

    }

    /**
     * 进度条控制
     */
    private void updateTimer(){
        new Thread(){
            @Override
            public void run(){
                while(true){
                    int progress = pb_timer_l.getProgress();
                    if(progress == 100){
                        progress = -10;
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pb_timer_l.setProgress(progress + 10);
                }
            }
        }.start();
    }

    private AnimUtil.AnimListener animListener = new AnimUtil.AnimListener() {
        boolean isDown = false;
        @Override
        public void down(View v) {

        }
        @Override
        public void move(View v) {

        }

        @Override
        public void up(View v) {
            switch (v.getId()){
                case R.id.btn_shop:
                    Log.i("MainActivity", "shop");
                    if(vp_content.getCurrentItem()!=1){
                        shopView.show();
                    }
                    vp_content.setCurrentItem(1);
                    rl_root.setBackgroundResource(R.mipmap.bg_shop);
                    break;
                case R.id.btn_home:
                    Log.i("MainActivity", "shouye");
                    addAnimationListener(btn_home);
                    break;
                case R.id.fl_beibao:
                    Log.i("MainActivity", "beibao");
                    beiBaoView.show();
                    break;
                case R.id.fl_fold_btn:
                    int subHeight = ll_sub_btns.getHeight() - 52;
                    if (isDown){
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_TRANSLATIONY, AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 0f);
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 180f, 3600f);
                        AnimUtil.addObjectAnimation(ll_sub_btns, new String[]{AnimUtil.PROPERTY_TRANSLATIONY}, 0f);
                    }else {
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_TRANSLATIONY, AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, subHeight);
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 0f, 180f);
                        AnimUtil.addObjectAnimation(ll_sub_btns, new String[]{AnimUtil.PROPERTY_TRANSLATIONY}, subHeight);
                    }
                    isDown = !isDown;
                    break;
            }
        }
    };

    public void addAnimationListener(final View v){
        shopView.hide(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                switch (v.getId()){
                    case R.id.btn_home:
                        vp_content.setCurrentItem(0);
                        rl_root.setBackgroundResource(R.mipmap.background_png);
                        break;
                    case R.id.btn_level:
                        break;
                    case R.id.btn_raid:
                        break;
                    case R.id.btn_shop:
                        break;
                    case R.id.btn_friends:
                        break;
                    case R.id.btn_setting:
                        break;
                }

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        switch (v.getId()){

        }
    }

}
